from problem import WazeProblem
from our_utils import save_to, read_csv_file, load_map

'''Note: you may change the signatures of the RunExperiment methods,
as long as you update main accordingly'''


@save_to('output1.csv')
def RunExperiment1(road_map):
    '''You must implement this function.
     Runs experiment 1 as written in the report'''
    for source, goal in read_csv_file():
        p = WazeProblem(road_map, source, goal)
        yield source, goal, [p.set_weights(t).RunAstar()
                            for t in ('close', 'fast', (0.4, 0.4, 0.2))]


def RunExperiment2(road_map):
    '''You must implement this function.
     Runs experiment 2 as written in the report'''
    fmt = '0'.join(['{', '.w:.2f},{', '.total_nodes},{',
                    '.time},{', '.path_cost:.2f}'])
    ps = read_csv_file()
    with open('output2.csv', 'w') as f:
        for i in range(10):
            source, goal = ps[i]
            p = WazeProblem(road_map, source, goal)
            f.write('{},{}'.format(source, goal))
            for w in [0.55, 0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1]:
                p.set_weights((0.4, 0.4, 0.2))
                res = p.RunWeightedAstar(w)
                f.write(',' + fmt.format(res))
                f.flush()
            f.write('\n')


def RunExperiment3(road_map):
    '''You may implement this function.
     Runs experiment 3 as written in the report'''
    pass


if __name__ == '__main__':
    '''Main part. You may add to this, if you wish'''
    road_map = load_map()
    RunExperiment1(road_map)
    RunExperiment2(road_map)
    RunExperiment3(road_map)
